<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/11/16
 * Time: 10:31
 */

namespace app\api\controller;


use app\common\controller\Api;
use think\Db;

class Order extends Api
{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    //订单列表
    public function orderList(){
        $user_id = (int)input('post.user_id');
        $status  = (int)input('post.status');
        if(!$user_id){
            $this->error('user_id不能为空');
        }
        $where = $status ? ['status'=>$status] :'';
        $data['list'] = Db::name('order_sku')->where('id',$user_id)->where($where)->order('time desc')->select();
        foreach ($data['list'] as $k=>$v){
            $data['list'][$k] = Db::name('order_goods')->where('order_id',$v['order_id'])->select();
        }
        if (!empty($data['list'])){
            $this->error('无数据');
        }
        $this->success('请求成功',$data);
    }

    //订单详情
    public function orderInfo(){
        $user_id  = input('post.user_id');
        $order_id = input('post.order_id');
        if (!$user_id){
            $this->error('user_id不能为空');
        }
        if (!$order_id){
            $this->error('order_id不能为空');
        }
        $data['order'] = Db::name('order_sku')->where('order_id',$order_id)->find();
        if (!empty($data)){
            $this->error('订单不存在');
        }
        $data['goods'] = Db::name('order_goods')->where('order_id',$order_id)->select();
        $data['address'] = Db::name('address')->where('id',$data['order']['addr_id'])->find();
        if (!empty($data)){
            $this->error('无数据');
        }
        $this->success('请求成功',$data);
    }

    //下单页面
    public function orderNow(){
        $goods_id = (int)input('post.goods_id');
        $user_id =  (int)input('post.user_id');
        $number   = (int)input('post.number');
        $spec     = (int)input('post.spec');
        if (!$goods_id){
            $this->error('参数goods_id不能为空');
        }
        if (!$user_id){
            $this->error('参数user_id不能为空');
        }
        if (!$number){
            $this->error('参数number不能为空');
        }
        $ret = Db::name('member_user')->where('id',$user_id)->find();
        $data['address'] = $ret['addr_id'] ? Db::name('address')->where('id',$ret['addr_id'])->find() : null;
        $data['goods']   = Db::name('goods_spu')->where('id',$goods_id)->find();
        $data['total'] = $data['goods']['price'] * $number;
        if ($spec){
            $data['spec'] = Db::name('goods_spec')->where('id',$spec)->find();
            $data['total'] = $data['spec']['price'] * $number;
        }else{
            $data['spec'] = null;
        }
        $data['number'] = $number;
        if (!$data){
            $this->error('无数据');
        }
        $this->success('请求成功',$data);
    }

    //下单
    public function orderStar(){
        $user_id     = (int)input('post.user_id');
        $goods_id    = (int)input('post.goods_id');
        $number      = (int)input('post.number');
        $spec        = (int)input('post.spec_id');
        $address     = (int)input('post.addr_id');
        $discount_id = (int)input('post.discount_id');
        $remark      = trim(input('post.remark'));
        if (!$goods_id){
            $this->error('参数goods_id不能为空');
        }
        if (!$user_id){
            $this->error('参数user_id不能为空');
        }
        if (!$number){
            $this->error('参数number不能为空');
        }
        if (!$address){
            $this->error('参数addr_id不能为空');
        }
        $member = Db::name('member_user')->where('id',$user_id)->find();
        if (!empty($member)){
            $this->error('用户不存在');
        }
        $goods = Db::name('goods_spu')->where('id',$goods_id)->find();
        $price = $goods['price'];
        if ($spec){
            $ret = Db::name('goods_spec')->where($spec)->find();
            $price = $ret['price'];
            if ($ret['stock'] < $number){
                $this->error('库存不足');
            }
        }
        if ($goods['stock'] < $number){
            $this->error('库存不足');
        }
        $orderMoney = $price * $number;
        $payMoney   = $orderMoney;
        if ($discount_id){
            $ret = Db::name('discount')->where('id',$discount_id)->find();
            $payMoney = $orderMoney - $ret['money'];
        }
        if ($goods['ship'] == 1){
            $payMoney = $payMoney - $goods['ship_money'];
        }
        $order_id = date('Ymdhis') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
        $data = [
            'order_id'=>$order_id,
            'user_id' =>$user_id,
            'money'   =>$orderMoney,
            'pay_money'=>$payMoney,
            'status'=>0,
            'addr_id'=>$address,
            'time'=>time(),
            'collage'=>0,
            'remark' =>$remark
        ];
        $order = Db::name('order_sku')->insert($data);
        $goods_data = [
            'order_id'=>$data['order_id'],
            'goods_id'=>$goods['id'],
            'price'   =>$price,
            'spec_id' =>$spec,
            'number'  =>$number
        ];
        $order_goods = Db::name('order_goods')->insert($goods_data);
        $num   = $this->goodsNumber($goods['id'],$number,$spec,0);
        if (!$member['status'] && !$member['pid']){
            $branch = new Branch();
            $ret = $branch->brokerageAdd($data['user_id'],$member['pid'],$data['order_id'],$data['pay_money']);
        }
        if($order && $num && $order_goods){
             $this->success('请求成功',$data);
        }else{
            $this->success('请求失败');
        }
    }

    //取消支付
    public function dePay(){
        $user_id  = input('post.user_id');
        $order_id = input('post.order_id');
        if (!$user_id){
            $this->error('user_id不能为空');
        }
        if (!$order_id){
            $this->error('order_id不能为空');
        }
        $ret = Db::name('order_sku')->where(['user_id'=>$user_id,'order_id'=>$order_id])->find();
        $goods = Db::name('order_goods')->where('order_id',$order_id)->select();
        if (!$ret){
            $this->error('无数据');
        }
        if ($ret['collage'] == 0){
            $this->success('请求成功');
        }
        $collage = Db::name('collage_group')->where(['user_id'=>$user_id,'order_id'=>$order_id])->find();
        if ($collage){
            $del = Db::name('collage_group')->where(['user_id'=>$user_id,'order_id'=>$order_id])->delete();
            foreach ($goods as $k=>$v){
                $num   = $this->goodsNumber($v['goods_id'],$v['number'],$v['spec_id'],1);
            }
            if (!$del && !$num){
                $this->error('请求失败');
            }
            $this->success('请求成功');
        }else{
            $user = Db::name('member_user')->where('id',$user_id)->find();
            $order = Db::name('order_sku')->where(['user_id'=>$user_id,'order_id'=>$order_id])->find();
            $branch = new Branch();
            $retbranch = $branch->brokerageDel($user['user_id'],$user['pid'],$order['order_id'],$order['pay_money']);
            $order = Db::name('order_sku')->where(['user_id'=>$user_id,'order_id'=>$order_id])->delete();
            $collage = Db::name('collage_group')->where(['order_id'=>$order_id])->find();
            $collage = Db::name('collage_group')->where(['order_id'=>$order_id])->update(['people'=>$collage['people'] - 1]);
            $people  = Db::name('collage_people')->where(['user_id'=>$user_id,'order_id'=>$order_id])->delete();
            foreach ($goods as $k=>$v){
                $num   = $this->goodsNumber($v['goods_id'],$v['number'],$v['spec_id'],1);
            }
            if (!$order && !$collage && !$people){
                $this->error('请求失败');
            }
            $this->success('请求成功');
        }



    }

    //购物车列表
    public function carInfo(){
        $user_id = (int)input('post.user_id');
        if (!$user_id){
            $this->error('user_id不能为空');
        }
        $data['list'] = Db::name('goods_car')->where('user_id',$user_id)->select();
        foreach ($data['list'] as $k=>$v){
            $data['list'][$k]['goods'] = Db::name('goods_car')->where('user_id',$v['user_id'])->find();
            if ($v['spec_id']){
                $data['list'][$k]['spec'] = Db::name('goods_spec')->where('id',$v['spec_id'])->find();
            }else{
                $data['list'][$k]['spec'] = null;
            }
        }
        if (!empty($data)){
            $this->error('无数据');
        }
        $this->success('请求成功',$data);
    }

    //购物车结算
    public function carSet(){
        $user_id = input('post.user_id');
        $data    = input('post.data');
        if (!$user_id){
            $this->error('user_id不能为空');
        }
        if (!$data){
            $this->error('data不能为空');
        }
        foreach ($data as $k=>$v){
            $data[]['goods'] = Db::name('goods_spu')->where('id',$v['goods_id'])->find();
            $data[]['number'] = $v['number'];
            $ret = $data[]['goods']['price'] * $v['number'];
            if ($v['spec_id'] == null){
                $data[]['spec'] = null;
            }else{
                $data[]['spec'] = Db::name('goods_spec')->where('id',$v['spec_id'])->find();
                $ret = $data[]['spec']['price'] * $v['number'];
            }
            $data['total'] += $ret;
        }
        if (!$data){
            $this->error('无数据');
        }
        $this->success('请求成功',$data);
    }

    //购物车下单
    public function carStar(){
        $user_id = input('post.user_id');
        $addr_id = input('post.addr_id');
        $discount_id = input('post.discount_id');
        $remark  = trim(input('post.remark'));
        $data    = input('post.data');
        if (!$user_id){
            $this->error('user_id不能为空');
        }
        if (!$addr_id){
            $this->error('addr_id不能为空');
        }
        if (!$data){
            $this->error('data不能为空');
        }
        $order_id = date('Ymdhis') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
        foreach ($data as $k=>$v){
            $data[]['goods'] = Db::name('goods_spu')->where('id',$v['goods_id'])->find();
            $money = $data[]['goods']['price'] * $v['number'];
            if ($data[]['goods']['ship'] == 1){
                $data['ship_money'][] = $data[]['goods']['ship_money'];
            }else{
                $data['ship_money'] = false;
            }
            if ($v['spec_id'] == null){
                if ($data[]['goods']['number'] < $v['number']){
                    $this->error('库存不足');
                }
                $data[]['spec'] = null;
                $goods_data = [
                    'order_id'=>$order_id,
                    'goods_id'=>$data[]['goods']['id'],
                    'price'   =>$data[]['goods']['price'],
                    'spec_id' =>0,
                    'number'  =>$v['number']
                ];
                 Db::name('order_goods')->insert($goods_data);
                $ret = $this->goodsNumber($data[]['goods']['id'],$v['number'],$v['spec_id'],0);
            }else{
                $data[]['spec'] = Db::name('goods_spec')->where('id',$v['spec_id'])->find();
                if ($data[]['spec']['number'] < $v['number']){
                    $this->error('库存不足');
                }
                $ret = $data[]['spec']['price'] * $v['number'];
                $goods_data = [
                    'order_id'=>$order_id,
                    'goods_id'=>$data[]['goods']['id'],
                    'price'   =>$data[]['price']['price'],
                    'spec_id' =>$data[]['spec']['id'],
                    'number'  =>$v['number']
                ];
                Db::name('order_goods')->insert($goods_data);
                $ret = $this->goodsNumber($data[]['goods']['id'],$v['number'],$v['spec_id'],0);
            }
            $data['total'] += $money;
        }
        $payMoney = $data['total'];
        if ($data['ship_money']){
            $payMoney = max($data['ship_money']);
        }
        if($discount_id){
            $ret = Db::name('discount')->where('id',$discount_id)->find();
            $payMoney = $data['total'] - $ret['money'];
        }
        $order_insert = [
            'order_id'=>$order_id,
            'user_id' =>$user_id,
            'money'   =>$data['total'],
            'pay_money'=>$payMoney,
            'status'=>0,
            'addr_id'=>$addr_id,
            'time'=>time(),
            'collage'=>0,
            'remark' =>$remark
        ];
        $insert = Db::name('order_sku')->insert($order_insert);
        $member = Db::name('member_user')->where('id',$user_id)->find();
        if (!$member['status'] && !$member['pid']){
            $branch = new Branch();
            $ret = $branch->brokerageAdd($data['user_id'],$member['pid'],$data['order_id'],$data['pay_money']);
        }
        if (!$insert){
            $this->error('请求失败');
        }
        $this->success('请求成功',$order_insert);
    }



    //购物车新增
    public function carAdd(){
        $user_id  = (int)input('post.user_id');
        $goods_id = (int)input('post.goods_id');
        $spec_id  = (int)input('post.spec_id');
        $number   = (int)input('post.number');
        if (!$user_id){
            $this->error('user_id不能为空');
        }
        if (!$goods_id){
            $this->error('goods_id不能为空');
        }
        if (!$number){
            $this->error('number不能为空');
        }
        $ret = Db::name('goods_spu')->where('id',$goods_id)->find();
        $price = $ret['price'];
        if (!$spec_id){
            $ret  = Db::name('goods_spec')->where('id',$spec_id)->find();
            $price = $ret['price'];
        }
        $insert = [
            'user_id'=>$user_id,
            'goods_id'=>$goods_id,
            'spec_id' => $spec_id ? $spec_id : 0,
            'price'   => $price,
            'number'  => $number,
            'time'    =>time()
        ];
        $ret = Db::name('goods_car')->insert($insert);
        if (!$ret){
            $this->error('请求失败');
        }
        $this->success('请求成功');
    }

    //修改购物车数量
    public function addNumber(){
        $user_id = input('post.user_id');
        $goods_id = input('post.goods_id');
        $number   = input('post.number');
        if (!$user_id){
            $this->error('user_id不能为空');
        }
        if (!$goods_id){
            $this->error('goods_id不能为空');
        }
        if (!$number){
            $this->error('number不能为空');
        }
        $ret = Db::name('goods_car')->where(['user_id'=>$user_id,'goods_id'=>$goods_id])->find();
        $num = $ret['number'] + $number;
        if ($num <= 0){
            $this->error('数量不能为0或小于0');
        }
        $ret = Db::name('goods_car')->where(['user_id'=>$user_id,'goods_id'=>$goods_id])->update(['number'=>$num]);
        if (!$ret){
            $this->error('请求失败');
        }
        $this->success('请求成功');
    }

    //删除购物车
    public function carDel(){
        $user_id  = input('post.user_id');
        $goods_id = input('post.goods_id');
        if (!$user_id){
            $this->error('user_id不能为空');
        }
        if (!$goods_id){
            $this->error('goods_id不能为空');
        }
        if (is_array($goods_id)){
            foreach ($goods_id as $item){
                $ret = Db::name('goods_car')->where(['user_id'=>$user_id,'goods_id'=>$item])->delete();
            }
        }else{
            $ret = Db::name('goods_car')->where(['user_id'=>$user_id,'goods_id'=>$goods_id])->delete();
        }
        if (!$ret){
            $this->error('请求失败');
        }
        $this->success('请求成功');
    }

    //取消订单
    public function orderDel(){
        $user_id  = input('post.user_id');
        $order_id = input('post.order_id');
        if (!$user_id){
            $this->error('user_id不能为空');
        }
        if (!$order_id){
            $this->error('$order_id不能为空');
        }
        $user = Db::name('member_user')->where('id',$user_id)->find();
        $order = Db::name('order_sku')->where(['user_id'=>$user_id,'order_id'=>$order_id])->find();
        $goods = Db::name('order_goods')->where('order_id',$order_id)->select();
        $ret = Db::name('order_sku')->where(['user_id'=>$user_id,'order_id'=>$order_id])->delete();
        $retgoods = Db::name('order_goods')->where('order_id',$order_id)->delete();
        foreach ($goods as $k=>$v){
            $num   = $this->goodsNumber($v['goods_id'],$v['number'],$v['spec_id'],1);
        }
        $branch = new Branch();
        $retbranch = $branch->brokerageDel($user['user_id'],$user['pid'],$order['order_id'],$order['pay_money']);
        if (!$ret && !$retgoods && !$retbranch){
            $this->error('请求失败');
        }
        $this->success('请求成功');
    }

    //确认收货
    public function orderTrue(){
        $user_id  = input('post.user_id');
        $order_id = input('post.order_id');
        if (!$user_id){
            $this->error('user_id不能为空');
        }
        if (!$order_id){
            $this->error('$order_id不能为空');
        }
        $user = Db::name('member_user')->where('id',$user_id)->find();
        $ret = Db::name('order_sku')->where(['user_id'=>$user_id,'order_id'=>$order_id])->update(['status'=>4]);
        $branch = new Branch();
        $ret2 = $branch->brokerageEdit($user['user_id'],$user['pid'],$order_id,2);
        if (!$ret && !$ret2){
            $this->error('请求失败');
        }
        $this->success('请求成功');
    }


    /**
     * @param $goods_id  string 商品ID
     * @param $number   string 数量
     * @param $spec    string 规格
     * @param $status string  状态:0=加,1=减
     * @return int
     * @throws
     */
    //商品数量修改
    public function goodsNumber($goods_id,$number,$spec,$status){
        $upData = Db::name('goods_spu')->where('id',$goods_id)->find();
        if ($spec){
            $upData = Db::name('goods_spec')->where('user_id',$goods_id)->where('id',$spec)->find();
        }
        if ($status == 1){
            $stock = $upData['stock'] + $number;
            $sell  = $upData['sell'] - $number;
        }else{
            $stock = $upData['stock'] - $number;
            $sell  = $upData['sell'] + $number;
        }
        $ret = Db::name('goods_spu')->where('id',$goods_id)->update(['stock'=>$stock,'sell'=>$sell]);
        if ($spec){
            $ret = Db::name('goods_spec')->where('user_id',$goods_id)->where('id',$spec)->update(['stock'=>$stock,'sell'=>$sell]);
        }
        if (!$ret){
            return 0;
        }
        return 1;
    }
    //申请退款
    public function refund(){
        $user_id = input('post.user_id');
        $order_id = input('post.order_id');
        if (!$user_id){
            $this->error('user_id不能为空');
        }
        if (!$order_id){
            $this->error('order_id不能为空');
        }
        $data = Db::name('order_sku')->where('order_id',$order_id)->find();
        if (!empty($data)){
            $this->error('无数据');
        }
        $ret = Db::name('order_sku')->where('order_id',$order_id)->update(['status'=>5]);

        if (!$ret){
            $this->error('请求失败');
        }
        $this->success('请求成功');
    }
}